package com.nctu.dm.fpgrowth;

import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
public class TransactionTable {
	String name;
	//Vector<TransactionTable> Next;
	TransactionTable Before;
	int parentFrequency;
	public  Map<String, TransactionValuePair> Table;
	public Map<String, Integer> SortTool;
	public  Map<String, TransactionValuePair> SortedTable;
	
	public TransactionTable(String name,int f){
		//Next = new Vector<TransactionTable>();
		Before = null;			
		Table = new TreeMap<String, TransactionValuePair>(new Mycompare());
		SortTool = new TreeMap<String, Integer>(new Mycompare());
		this.name = name;
		this.parentFrequency = f;
	}
	
	public TransactionTable(String name,int f,Map<String, Integer> SortTool){
		//Next = new Vector<TransactionTable>();
		Before = null;			
		Table = new TreeMap<String, TransactionValuePair>(new Mycompare());
		this.SortTool = SortTool;
		this.name = name;
		this.parentFrequency = f;
	}
	
	public void addTransaction(String id,Vector<String> values,int count){
		TransactionValuePair p= new TransactionValuePair(count,values);
		Table.put(id, p);
	}
	
	public void addTransaction(String id,String value,int count){
		TransactionValuePair p= new TransactionValuePair(count);
		p.trans.add(value);
		Table.put(id, p);
	}	
	
	public void addTransaction(String id,TransactionValuePair tvp){		
		Table.put(id, tvp);
	}		
	
	public void addTransaction(String id,String value){
		if(Table.containsKey(id)){
			Table.get(id).trans.add(value);
		}else{
			TransactionValuePair p= new TransactionValuePair(1);
			p.trans.add(value);
			Table.put(id, p);			
		}
	}	
	
	
	
	private class CompareBySortedTable implements Comparator{
		   private int priority(String s){
			   if(SortTool.containsKey(s))				   
			   { 
				   System.out.println(SortTool.get(s));
				   return SortTool.get(s);}
			   else
				   return -1;
		   }
	       public int compare(Object obj1, Object obj2){
	           String o1=(String) obj1;
	           String o2=(String) obj2;
	           
	           
	           int p = priority(o1) - priority(o2);
	           if(p==0)
	           {
	        	   return o1.compareTo(o2);
	           }
	           else
	        	   return -p;	        
	       }
	}

	public void Sort(int mSupport){
		//Create Sort Table
		long StartTime = System.currentTimeMillis();
		Map<String, Integer> TempSortedTable = new TreeMap<String, Integer>(new Mycompare());
		SortedTable = new TreeMap<String, TransactionValuePair>(new Mycompare());
		for( TransactionValuePair p:Table.values()){
			for (String s:p.trans){
				if(TempSortedTable.containsKey(s)){
					int kt = TempSortedTable.get(s);
					kt+=p.count;
					TempSortedTable.put(s, kt);
				}else{
					TempSortedTable.put(s, p.count);
				}				
			}			
		}		
		
		
		//delete none-approximate entry which value < min surpport
		
		for(Map.Entry<String,Integer> e : TempSortedTable.entrySet()){	
			if(e.getValue() >= mSupport){ 					
				SortTool.put(e.getKey(),e.getValue());
			}
		}			
		//sort the TransactionTable by SortTool
		for( Map.Entry<String,TransactionValuePair>  e: Table.entrySet()){
			Vector<String> temp = new Vector<String>();
			for (String s:e.getValue().trans){
				if(SortTool.containsKey(s)){
					temp.add(s);
				}				
			}
			if(temp.size()!=0){ 
				Collections.sort(temp, new CompareBySortedTable());
				TransactionValuePair p= new TransactionValuePair(e.getValue().count,temp);
				SortedTable.put(e.getKey(), p );
			}
		}
		Table = SortedTable;
		if(Common.Debug)
		{
			this.ToString();
		}
		long ProcessTime = System.currentTimeMillis() - StartTime;
		if(Common.TimeDebug >0) 
			System.out.printf("Table sort time = %ds\n",ProcessTime/1000);
	}
	/*
	public void Sort(int mSupport, Map<String, Integer> SortTool){		
		//sort the TransactionTable by SortedTable
		//Map<String, Integer> TempSortedTable = new TreeMap<String, Integer>(new Mycompare());
		SortedTable = new TreeMap<String, TransactionValuePair>(new Mycompare());				
		for( Map.Entry<String,TransactionValuePair>  e: Table.entrySet()){
			Vector<String> temp = new Vector<String>();
			for (String s:e.getValue().trans){
				if(SortTool.containsKey(s)){
					temp.add(s);
				}				
			}
			if(temp.size()!=0){ 
				Collections.sort(temp, new CompareBySortedTable());
				TransactionValuePair p= new TransactionValuePair(e.getValue().count,temp);
				SortedTable.put(e.getKey(), p );
			}
			temp = null;
		}
		Table = SortedTable;
		if(Common.Debug)
		{
			this.ToString();
		}
	}	
	*/
	public void ToString(){
		for( Map.Entry<String,TransactionValuePair>  e: SortedTable.entrySet()){
			System.out.printf("%s ",e.getKey());
			for (String s:e.getValue().trans){
				System.out.printf("%s,",s);
			}
			System.out.printf(":%s",e.getValue().count);
			System.out.println();
		}
	}
	/*
	public void DeallocateSources()
	{
		SortedTable = null;
		Table = null;
		SortTool = null;
	}
	*/	
		
	
}
